<?php
/**
 * Created by PhpStorm.
 * User: luoshuqi
 * Date: 19-2-14
 * Time: 下午5:25
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\SysUser;
use App\Utils\CaptchaTrait;
use Illuminate\Http\Request;

/**
 * 注意：本控制器的方法不检查权限
 * Class IndexController
 * @package App\Http\Controllers\Admin
 */
class IndexController extends Controller
{
    use CaptchaTrait;

    /**
     * 菜单
     * @return array
     */
    public function menu()
    {
        $menu = [
            ['title' => '系统管理', 'index' => 'sys', 'icon' => 'gear', 'children' => [
                ['title' => '管理员', 'index' => '/sys/user', 'icon' => 'user'],
                ['title' => '用户组', 'index' => '/sys/group', 'icon' => 'users'],
                ['title' => '操作日志', 'index' => '/sys/log', 'icon' => 'archive'],
            ]],
        ];

        return $this->ok($menu);
    }

    /**
     * 获取配置
     * @return array
     */
    public function config()
    {
        $config = [
            'captcha' => $this->displayCaptcha(),
        ];
        return $this->ok($config);
    }

    /**
     * 获取验证码
     */
    public function captcha()
    {
        return $this->ok($this->buildCaptcha());
    }

    /**
     * 修改密码
     * @param Request $request
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public function alterPassword(Request $request)
    {
        list('password' => $password, 'new_password' => $newPassword) =
            $this->required($request, 'password', 'new_password');

        /** @var SysUser $user */
        $user = $request->attributes->get('user');
        if ($user->password($password) === $user['password']) {
            $user['password'] = $newPassword;
            $user->save();
            return $this->ok();
        } else {
            return $this->error('密码错误');
        }
    }
}